# #找出基因
# def find_gene(s):
#     start = s.find('ATG') + 3
#     last = ['TAG', 'TAA', 'TGA']
#     min_end = len(s)
#     for mapper in last:
#         if mapper in s:
#             end = s.find(mapper)
#             if end != -1 and end < min_end:
#                 min_end = end
#     if min_end == len(s):
        
    
#     # for i in range(start, end):
#     #     print(s[i], end = '')
#     str = ''
#     for i in range(start, end):
#         str += s[i]
#     print(str)

#输入基因组序列
# s = input()
# genes = []
# start = 0
# #合法字符集
# valid_chars = {'A', 'C', 'T', 'G'}
# while True:
#     start = s.find('ATG', start)
#     #没找都基因组
#     if start == -1:
#         break
#     #基因组开始的位置跳过ATG
#     start += 3
#     last = ['TAG', 'TAA', 'TGA']
#     #查找最近的三元组
#     min_end = len(s)
#     for mapper in last:
#         end = s.find(mapper, start)
#         if end != -1 and end < min_end:
#             min_end = end
#     #找不到终止三元组        
#     if min_end == len(s):
#         break
#     #提取候选基因
#     candidate = s[start: min_end]
#     # print(len(candidate))
#     #检查(基因长度是否为3的倍数，并且不包含禁止的三元组
#     if len(candidate) % 3 == 0:
#         forbidden = ['ATG', 'TAG', 'TAA', 'TGA']
#         valid = True
#         if not all(c in valid_chars for c in candidate):
#             valid = False
#         for i in range(0, len(candidate), 3):
#             if candidate[i:i+3] in forbidden:
#                 valid = False
#                 break
#         if valid:
#             genes.append(candidate)
# if not genes:
#     print('未发现任何基因')
# else:
#     for gene in genes:
#         print(gene)

    
# #手机键盘
# digit_to_letters = { 
#     '2' : 'abc',
#     '3' : 'def',
#     '4' : 'ghi',
#     '5' : 'jkl',
#     '6' : 'mno',
#     '7' : 'pqrs',
#     '8' : 'tuv',
#     '9' : 'wxyz',
#     '0' : ' ',
#     '↑' : '*',
#     '*' : '+'
# }
# #生成字母到数字的反向映射字典（字母为键数字为值）
# letter_to_digit = {}
# for digit, letters in digit_to_letters.items():
#     for letter in letters:
#         letter_to_digit[letter] = digit

# s = input().lower()
# result = []
# #遍历用户输入的字符串
# for char in s:
#     #如果在映射字典里就添加对应的数字键
#     if char in letter_to_digit:
#         result.append(letter_to_digit[char]) 
#     #如果是数字字符直接保留
#     elif char.isdigit():
#         result.append(char)
# #用join将列表中的字符串连接起来
# print(''.join(result))

#信用卡号的合法性
def is_law(digit):
    if len(digit) < 13 or len(digit) > 16:
        return False
    #如果不是以这些数字开头就直接返回False
    if not digit.startswith(('4', '5', '37', '6')):
        return False
    the_sum = 0
    for i in range(0, len(digit), 2):
        by = int(digit[i]) * 2
        # print(by, end = " ")
        if by > 9:
            the_sum += (by % 10 + by // 10)
        else:
            the_sum += by
    #将第二部得到的所有一位数相加
    two_sum = 0
    for i in range(len(digit) - 1, -1, -2):
        # print(digit[i], end = ' ')
        two_sum += int(digit[i])
    # print(the_sum, two_sum)
    if (the_sum + two_sum) % 10 == 0:
        return True
    else:
        return False
digit = input()
if is_law(digit):
    print('合法')
else:
    print('不合法')
#将第一步得到的所有一位数相加
# the_sum = 0
# for i in range(len(digit)):
#     by = int(digit[i]) * 2
#     if by > 9:
#         the_sum += (by % 10 + by // 10)
#     else:
#         the_sum += by
# #将第二部得到的所有一位数相加
# two_sum = 0
# for i in range(len(digit) - 1, -1, -2):
#     two_sum += int(digit[i])
# #将第二步与第三步结果相加能被10整除就合法
# if (the_sum + two_sum) % 10 == 0:
#     print('合法')
# else:

